package org.gdstash.ui.font;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class EscapeDialog extends JDialog {
    protected JButton okButton;

    public EscapeDialog() {
    }

    public EscapeDialog(final Dialog owner, final boolean modal) {
        super(owner, modal);
    }

    public EscapeDialog(final Dialog owner, final String title, final boolean modal, final GraphicsConfiguration gc) {
        super(owner, title, modal, gc);
    }

    public EscapeDialog(final Dialog owner, final String title, final boolean modal) {
        super(owner, title, modal);
    }

    public EscapeDialog(final Dialog owner, final String title) {
        super(owner, title);
    }

    public EscapeDialog(final Dialog owner) {
        super(owner);
    }

    public EscapeDialog(final Frame owner, final boolean modal) {
        super(owner, modal);
    }

    public EscapeDialog(final Frame owner, final String title, final boolean modal, final GraphicsConfiguration gc) {
        super(owner, title, modal, gc);
    }

    public EscapeDialog(final Frame owner, final String title, final boolean modal) {
        super(owner, title, modal);
    }

    public EscapeDialog(final Frame owner, final String title) {
        super(owner, title);
    }

    public EscapeDialog(final Frame owner) {
        super(owner);
    }

    public EscapeDialog(final Window owner, final ModalityType modalityType) {
        super(owner, modalityType);
    }

    public EscapeDialog(final Window owner, final String title, final ModalityType modalityType, final GraphicsConfiguration gc) {
        super(owner, title, modalityType, gc);
    }

    public EscapeDialog(final Window owner, final String title, final ModalityType modalityType) {
        super(owner, title, modalityType);
    }

    public EscapeDialog(final Window owner, final String title) {
        super(owner, title);
    }

    public EscapeDialog(final Window owner) {
        super(owner);
    }

    @Override
    protected JRootPane createRootPane() {
        final JRootPane rootPane = new JRootPane();
        final KeyStroke escapeStroke = KeyStroke.getKeyStroke("ESCAPE");
        final Action escapeActionListener = new AbstractAction() {
            @Override
            public void actionPerformed(final ActionEvent event) {
                EscapeDialog.this.setVisible(false);
            }
        };
        final KeyStroke enterStroke = KeyStroke.getKeyStroke("ENTER");
        final Action enterActionListener = new AbstractAction() {
            @Override
            public void actionPerformed(final ActionEvent event) {
                EscapeDialog.this.okButton.doClick();
            }
        };
        final InputMap inputMap = rootPane.getInputMap(2);
        inputMap.put(escapeStroke, "ESCAPE");
        inputMap.put(enterStroke, "ENTER");
        rootPane.getActionMap().put("ESCAPE", escapeActionListener);
        rootPane.getActionMap().put("ENTER", enterActionListener);
        return rootPane;
    }

    public JButton getOkButton() {
        return this.okButton;
    }

    public void setOkButton(final JButton okButton) {
        this.okButton = okButton;
    }
}
